package site.marqstree.latte.ec.launch;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.text.MessageFormat;
import java.util.Timer;

import butterknife.BindView;
import butterknife.OnClick;
import site.marqstree.latte.ec.R;
import site.marqstree.latte.ec.R2;
import site.marqstree.latte.ec.sign.SignInDelegate;
import site.marqstree.latte.core.app.AccountManager;
import site.marqstree.latte.core.app.IUserChecker;
import site.marqstree.latte.core.delegates.BaseDelegate;
import site.marqstree.latte.core.delegates.LatteDelegate;
import site.marqstree.latte.core.storage.LattePreference;
import site.marqstree.latte.core.util.timer.BaseTimerTask;
import site.marqstree.latte.core.util.timer.ITimerListener;
import site.marqstree.latte.ui.launch.ILauncherListener;
import site.marqstree.latte.ui.launch.OnLauncherFinishTag;
import site.marqstree.latte.ui.launch.ScrollLauncherTag;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.launch
 * 文件名: LauncherDelegate
 * 创建者: marqstree
 * 创建时间: 2020/1/16 19:38
 * 描述: 倒计时启动界面
 */
public class LauncherDelegate extends LatteDelegate implements ITimerListener {

    @BindView(R2.id.tv_launcher_timer)
    TextView mTvTimer = null;
    private Timer mTimer = null;
    //倒计时长
    //单位：s
    private int mCount = 5;
    private ILauncherListener mILauncherListener = null;

    //绑定布局文件
    @Override
    public Object setLayout() {
        return R.layout.delegate_launcher;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        //启动定时任务
        initTimer();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ILauncherListener) {
            mILauncherListener = (ILauncherListener) activity;
        }
    }

    //点击关闭倒计时任务
    //然后检查是否要开启轮播启动页
    @OnClick(R2.id.tv_launcher_timer)
    void onClickTimerView() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
            //检查是否要开启轮播启动页
            checkIsShowScroll();
        }
    }

    //启动倒计时任务
    private void initTimer() {
        //创建定时器
        mTimer = new Timer();
        //创建自定义定时器任务，执行任务接口：onTimer函数
        final BaseTimerTask task = new BaseTimerTask(this);
        //无延迟，立即启动定时任务，每1秒执行自定义任务接口函数onTimer
        mTimer.schedule(task, 0, 1000);
    }

    //自定义定时执行任务接口函数
    @Override
    public void onTimer() {
        //在UI主线程中执行刷新UI倒计时操作
        getProxyActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //刷新UI中的倒计时
                if (mTvTimer != null) {
                    mTvTimer.setText(MessageFormat.format("跳过\n{0}s", mCount));
                    mCount--;
                    //倒计时完后，关闭定时器，关闭定时任务
                    if (mCount < 0) {
                        if (mTimer != null) {
                            mTimer.cancel();
                            mTimer = null;
                            //检查是否开启轮播启动页
                            checkIsShowScroll();
                        }
                    }
                }
            }
        });
    }

    //判断是否显示滑动启动页
    private void checkIsShowScroll() {
        //第一次启动App时，启动轮播启动页
        if (!LattePreference.getAppFlag(ScrollLauncherTag.HAS_FIRST_LAUNCHER_APP.name())) {
            getSupportDelegate().start(new LauncherScrollDelegate(), SINGLETASK);
        }else{
            //检查用户是否登录了APP
            AccountManager.checkAccount(new IUserChecker() {
                @Override
                public void onSignIn() {
                    if (mILauncherListener != null) {
                        mILauncherListener.onLauncherFinish(OnLauncherFinishTag.SIGNED);
                    }
                }

                @Override
                public void onNotSignIn() {
                    if (mILauncherListener != null) {
                        mILauncherListener.onLauncherFinish(OnLauncherFinishTag.NOT_SIGNED);
                    }
                }
            });
        }
    }
}